import type { MenuDataItem } from "@/constants";
import { create } from "zustand";
import { createJSONStorage, persist } from "zustand/middleware";

interface State {
  name: string;
  token: string;
  keyPath: string[];
  menuList: MenuDataItem[];
  menuTree: MenuDataItem[];
  collapsed: boolean;
}
interface Action {
  setName: (state: string) => void;
  setToken: (state: string) => void;
  setKeyPath: (state: string[]) => void;
  setMenuList: (state: MenuDataItem[]) => void;
  setMenuTree: (state: MenuDataItem[]) => void;
  setCollapsed: (state: boolean) => void;
}

// 应用核心数据
export const useMainStore = create(
  persist<State & Action>(
    (set) => ({
      name: "",
      token: "",
      keyPath: [],
      menuList: [],
      menuTree: [],
      collapsed: false,
      setName: (state) => {
        set({ name: state });
      },
      setToken: (state) => {
        set({ token: state });
      },
      setKeyPath: (state) => {
        set({ keyPath: state });
      },
      setMenuList: (state) => {
        set({ menuList: state });
      },
      setMenuTree: (state) => {
        set({ menuTree: state });
      },
      setCollapsed: (state) => {
        set({ collapsed: state });
      }
    }),
    // 持久化
    {
      name: "mainStore",
      storage: createJSONStorage(() => sessionStorage)
    }
  )
);
